package com.jiuwu.spring.eshop.controller;

import com.jiuwu.spring.eshop.db.entity.SubType;
import com.jiuwu.spring.eshop.db.entity.SuperComSub;
import com.jiuwu.spring.eshop.db.entity.SuperType;
import com.jiuwu.spring.eshop.respository.SubTypeRepository;
import com.jiuwu.spring.eshop.respository.SuperTypeRepository;
import com.jiuwu.spring.eshop.utils.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/manage")
public class SubTypeController {

    @Autowired
    SubTypeRepository subTypeRepository;

    @Autowired
    SuperTypeRepository superTypeRepository;

    @RequestMapping("/subType")
    public String getSubType(Model model){
        List<Object[]> objectList = subTypeRepository.findAllSuperComSub();
        List<SuperComSub> superComSubList = EntityUtils.castEntity(objectList, SuperComSub.class, new SuperComSub());
        model.addAttribute("comList",superComSubList);
        return "/manage/subType";
    }

    @RequestMapping("/sub_add")
    public String loadSubType(Model model){
        List<SuperType> superTypeList = superTypeRepository.findAll();
        model.addAttribute("superTypeList",superTypeList);
        return "/manage/sub_add";
    }

    @RequestMapping("/subType_add_deal")
    public String addSubType(@RequestParam("superid")int superId,
                             @RequestParam("typename")String typeName ){
        SuperType superType = new SuperType();
        superType.setID(superId);
        SubType subType = new SubType();
        subType.setSuperType(superType);
        subType.setTypeName(typeName);
        subTypeRepository.save(subType);
        return "redirect:/manage/index?id=7";
    }

    @RequestMapping("/subType_del_deal")
    public String delSuperType(HttpServletRequest request){
        for (String delid : request.getParameterValues("delid")) {
            Integer id = Integer.valueOf(delid);
            subTypeRepository.deleteById(id);
            System.out.println("delete id :"+id);
        }

        return "redirect:/manage/index?id=7";
    }
















    //-------------------------thymeleaf----------------------------------------


    @GetMapping("/subTypes")
    public String getSubTypes(Model model){
        List<Object[]> objectList = subTypeRepository.findAllSuperComSub();
        List<SuperComSub> superComSubList = EntityUtils.castEntity(objectList, SuperComSub.class, new SuperComSub());
        model.addAttribute("comList",superComSubList);
        return "goods/subType";
    }

    //进入小分类添加页面
    @GetMapping("/sbType")
    public String loadSubType2(Model model){
        List<SuperType> superTypeList = superTypeRepository.findAll();
        model.addAttribute("superTypeList",superTypeList);
        return "goods/addSubtype";
    }

    //添加小分类
    @PostMapping("/sbType")
    public String addSubType2(@RequestParam("superId")int superId,
                             @RequestParam("typeName")String typeName ){


        SuperType superType = new SuperType();
        superType.setID(superId);
        SubType subType = new SubType();
        subType.setSuperType(superType);
        subType.setTypeName(typeName);
        subTypeRepository.save(subType);
        return "redirect:/manage/subTypes";
    }

    //删除小分类
    @GetMapping("/delSubType/{subTypeId}")
    public String delSubType(@PathVariable("subTypeId") Integer id) {
        subTypeRepository.deleteById(id);
        return "redirect:/manage/subTypes";
    }
}
